<?php

# Setup classes
include_once(dirname(__FILE__).'/ajaxResource.php');

# Define variables
$return = array('results' => '');
$results = array();
$invalidInput = false;
		
# Query resources
$resources = $digitalUniversity->db->getResults("SELECT *, 'resource' AS aType FROM resources 
WHERE isApproved = '1' AND title LIKE '%{$digitalUniversity->post['keyword']}%'
ORDER BY time DESC
LIMIT 4");

# Query modules
$books = $digitalUniversity->db->getResults("SELECT *, 'book' AS aType FROM books 
WHERE title LIKE '%{$digitalUniversity->post['keyword']}%'
ORDER BY cacheTime DESC
LIMIT 4");

# Found resources
if(!empty($resources)) {
	foreach($resources as $k => $v) {
		$results[$v['time']] = $v;	
	}
}

# Found books
if(!empty($books)) {
	foreach($books as $k => $v) {
		$results[$v['cacheTime']] = $v;		
	}
}

# Output results
if(!empty($results)) {
	foreach($results as $k => $v) {
		if($v['aType'] == 'resource') {
			$return['results'] .= '<div class="searchResults resultsResource"><a href="'.$digitalUniversity->config['scripturl'].'page=resources&resource='.$v['resourceID'].'">'.$v['type'].' - '.$v['title'].'</a></div>';
		} else {
			$return['results'] .= '<div class="searchResults resultsBook"><a href="'.$digitalUniversity->config['scripturl'].'page=book&book='.$v['ISBN'].'">'.$v['title'].'</a></div>';
		}
	}
	
}

# Return response
echo(json_encode($return));

# Release memory
unset($digitalUniversity);

?>